﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using XcoAppSpaces.Contracts.WorkerResolve;
using XcoAppSpaces.Core;

namespace XcoAppSpaces.Serialization.Binary
{
	class ResolveResponseSerializationSurrogate : ISerializationSurrogate
	{
		public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
		{
			var msg = (ResolveResponse)obj;
			info.AddValue("Error", msg.Error);
			info.AddValue("ProcessorAttributes", msg.ProcessorAttributes);
			info.AddValue("RequestID", msg.RequestID);
			info.AddValue("SessionID", msg.SessionID);
			info.AddValue("Status", (byte)msg.Status);
		}

		public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
		{
			obj = new ResolveResponse
				{
					Error = (Exception)info.GetValue("Error", typeof(Exception)),
					ProcessorAttributes = (Dictionary<Type, XcoProcessorAttribute>)info.GetValue("ProcessorAttributes", typeof(Dictionary<Type, XcoProcessorAttribute>)),
					RequestID = (Guid)info.GetValue("RequestID", typeof(Guid)),
					SessionID = (Guid)info.GetValue("SessionID", typeof(Guid)),
					Status = (ResolveStatus)(byte)info.GetValue("Status", typeof(byte)),
				};
			return obj;
		}
	}
}